package ru.tatintec.gwttest.client.presenter;

import java.util.List;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;

import ru.tatintec.gwttest.client.service.EmployeeCatalogServiceAsync;
import ru.tatintec.gwttest.client.service.ServiceHelper;
import ru.tatintec.gwttest.client.view.EmployeeListComposite;
import ru.tatintec.gwttest.client.view.EmployeeListComposite.DeleteEditPanel;
import ru.tatintec.gwttest.shared.Employee;

public class EmployeeListPresenter {

	private EmployeeCatalogServiceAsync employeeService = ServiceHelper.getEmployeeCatalogService();
	private EmployeeListComposite ui = new EmployeeListComposite(); 
	private DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("dd.MM.yyyy");
	
	public EmployeeListPresenter() {
		refreshEmployeeList();
		ui.getAddButton().addClickHandler(getAddHandler());
		ui.getGenerateButton().addClickHandler(getGenerateHandler());
	}


	private ClickHandler getGenerateHandler() {
		ClickHandler handler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				ServiceHelper.getEmployeeCatalogService().generateTestData(new AsyncCallback<Void>() {
					@Override
					public void onSuccess(Void result) {
						refreshEmployeeList();
					}
					@Override
					public void onFailure(Throwable caught) {
						// TODO
						Window.alert(caught.getMessage());
					}
				});
			}
		};
		return handler;
	}


	private void refreshEmployeeList() {
		final FlexTable t = ui.getTable();
		final Label noDataLabel = ui.getNoDataLabel();
		t.setVisible(false);
		employeeService.getEmployeeList(new AsyncCallback<List<Employee>>() {
			@Override
			public void onSuccess(List<Employee> result) {
				t.clear(true);
				if (result != null && result.size() > 0) {
					ui.addTableHeader();

					int row = 1;
					for (Employee employee : result)  {
						t.setText(row, 0, ""+row);
						t.setText(row, 1, employee.getLFM());
						t.setText(row, 2, employee.getSex().toString());
						t.setText(row, 3, dateTimeFormat.format(employee.getBirthday()));
						t.setText(row, 4, dateTimeFormat.format(employee.getEmploymentDate()));
						
						DeleteEditPanel deleteEditPanel = ui.new DeleteEditPanel();
						deleteEditPanel.getDeleteButton().addClickHandler(getDeleteHandler(employee.getId()));
						deleteEditPanel.getEditButton().addClickHandler(getEditHandler(employee));
						
						t.setWidget(row, 5, deleteEditPanel);  
						
						row++;
					}
					t.setVisible(true);
					noDataLabel.setVisible(false);
				} else {
					t.setVisible(false);
					noDataLabel.setVisible(true);
				}
			}
			@Override
			public void onFailure(Throwable caught) {
				// TODO ???
//				t.setVisible(true);
//				noDataLabel.setVisible(false);
				Window.alert(caught.getMessage());
			}
		});
	}
	
	
	protected ClickHandler getAddHandler() {
		ClickHandler editClickHandler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final CreateEditPresenter createPresenter = new CreateEditPresenter();
				createPresenter.create(new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						createPresenter.unlockDialog();
						Window.alert(caught.getMessage());
					}
					@Override
					public void onSuccess(Void result) {
						createPresenter.close();
						refreshEmployeeList();						
					}
				});
			}
		};
		return editClickHandler;
	}

	
	protected ClickHandler getEditHandler(final Employee employee) {
		ClickHandler editClickHandler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final CreateEditPresenter editPresenter = new CreateEditPresenter();
				editPresenter.edit(employee, new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
						editPresenter.unlockDialog();
						// TODO Auto-generated method stub
						Window.alert(caught.getMessage());
					}
					@Override
					public void onSuccess(Void result) {
						editPresenter.close();
						refreshEmployeeList();						
					}
				});
			}
		};

		return editClickHandler;
	}


	protected ClickHandler getDeleteHandler(final String id) {
		ClickHandler deleteClickHandler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				
				if (!Window.confirm("Do you really want to delete?")) return;
				
				employeeService.deleteEmployee(id, new AsyncCallback<Void>() {
					@Override
					public void onSuccess(Void result) {
						refreshEmployeeList();
					}
					@Override
					public void onFailure(Throwable caught) {
						// TODO
						Window.alert(caught.getMessage());
					}
				});
			}
		};
		return deleteClickHandler;
	}


	public EmployeeListComposite getUi() {
		return ui;
	}

}
